package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.ums.entity.UmsApplyLog;
import org.jeecg.modules.job.ums.entity.UmsApplyRead;
import org.jeecg.modules.job.ums.service.IUmsApplyLogService;
import org.jeecg.modules.job.ums.service.IUmsApplyReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *投递简历模块管理
 */
@RestController
@RequestMapping("/api/apply")
@Api(tags="移动端投递简历管理模块")
@Slf4j
public class ApplyController{

    @Autowired
    private IUmsApplyLogService applyLogService;
    @Autowired
    private IUmsApplyReadService applyReadService;

    /**
     * 查询投递列表
     */
    @RequestMapping(value = "/getApplyList",method = RequestMethod.GET)
    @ApiOperation(value = "查询投递列表", notes = "查询投递列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public Result<Object> getApplyList(UmsApplyLog param,
                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
    ) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        param.setMemberId(user.getId());
        IPage<UmsApplyLog> pageInfo= applyLogService.findPageBySpec(new Page<>(pageNo,pageSize),param);
        return Result.OK(pageInfo);
    }

    /**
     * 查询投递详情
     */
    @RequestMapping(value = "/getApplyDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询投递详情", notes = "查询投递详情")
    public Result<Object> getApplyDetail(String id) {
        UmsApplyLog result=applyLogService.getById(id);
        return Result.OK(result);
    }

    /**
     * 添加/更新简历投递
     */
    @RequestMapping(value = "/addOrUpdateApply",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新简历投递", notes = "添加/更新简历投递")
    public Result<Object> addOrUpdateApply(@RequestBody UmsApplyLog param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            applyLogService.addOrUpdate(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    /**
     * 删除简历投递
     */
    @RequestMapping(value = "/deleteApply",method = RequestMethod.POST)
    @ApiOperation(value = "删除简历投递", notes = "删除简历投递")
    public Result<Object> deleteApply(@RequestBody UmsApplyLog param) {
        try {
            applyLogService.removeById(param.getId());
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加/更新阅读记录
     */
    @RequestMapping(value = "/addApplyRead",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新简历阅读记录", notes = "添加/更新简历阅读记录")
    public Result<Object> addApplyRead(@RequestBody UmsApplyRead param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            applyReadService.addOrUpdate(param);
            return Result.ok();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新简历状态（企业端操作）
     */
    @ResponseBody
    @RequestMapping(value = "/updateApplyStatus",method = RequestMethod.POST)
    @ApiOperation(value = "更新简历状态", notes = "更新简历状态")
    public Result<Object> updateApplyStatus(@RequestBody UmsApplyLog param) {
        try {
            applyLogService.updateApplyStatus(param);
            return Result.ok();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}



